/**
 * @file useLed.c
 * @brief
 * @author  xiaowine (xiaowine@sina.cn)
 * @version 01.00
 * @date    2021-04-10
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2021-04-10 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

/* Private includes ----------------------------------------------------------*/
#include "userLed.h"
#include <string.h>
#include "cmsis_os2.h"
#include "stm32g0xx_hal.h"
#include "user_data.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/********************************************************************/
#define LEDNUM 20
unsigned char ledFlashFast[LEDNUM] = {0};

volatile _USR_FLAGA_type ledState[LEDBUFFNUM];
uint32_t                 ledValue = 0;
/* Private function prototypes -----------------------------------------------*/
static void ledSetState(unsigned char num, GPIO_PinState state);
/* Private user code ---------------------------------------------------------*/

/**
 * @brief
 */
void ledInit(void) {}
void userLed(void)
{
    if (FLAG_250MSF) {
        if (flashFlag_2HZ) {
            flashFlag_2HZ = 0;
        } else {
            flashFlag_2HZ = 1;
        }
    }
    if (FLAG_500MSF) {
        // RUN = !RUN;
        if (flashFlag_1HZ) {
            flashFlag_1HZ = 0;
        } else {
            flashFlag_1HZ = 1;
        }
        if (flashFlag_1HZ) {
            if (flashFlag_0_5HZ) {
                flashFlag_0_5HZ = 0;
            } else {
                flashFlag_0_5HZ = 1;
            }
        }
    }

    /***************************/
    if (FLAG_63MSF || FLAG_250MSF || FLAG_500MSF) {
        unsigned char i, value, State;
        uint32_t      ledValueTemp = ledValue;
        for (i = 0; i < LEDNUM; i++) {
            value = ((ledValueTemp >> i) & 1);
            if (i % 2 == 0) {
                State = ledState[i / 2].byte & 0x0f;
            } else {
                State = (ledState[i / 2].byte >> 4);
            }

            if (ledFlashFast[i] > 0) {
                if (State >= STATE_LED_FLASH_1_T) {
                    ledFlashFast[i] += 2 * (State - STATE_LED_FLASH_0_5HZ);
                    if (i % 2 == 0) {
                        ledState[i / 2].byte &= 0xf0;
                    } else {
                        ledState[i / 2].byte &= 0x0f;
                    }
                }
                if (FLAG_63MSF) {
                    ledFlashFast[i]--;
                    value = !((ledValueTemp >> i) & 1);
                }
            } else {
                switch (State) {
                    case STATE_LED_OFF:
                        value = LED_OFF;
                        break;
                    case STATE_LED_ON:
                        value = LED_ON;
                        break;
                    case STATE_LED_FLASH_2HZ:
                        if (FLAG_250MSF)
                            value = flashFlag_2HZ;
                        break;
                    case STATE_LED_FLASH_1HZ:
                        if (FLAG_500MSF)
                            value = flashFlag_1HZ;
                        break;
                    case STATE_LED_FLASH_0_5HZ:
                        if (FLAG_500MSF)
                            value = flashFlag_0_5HZ;
                        break;
                    case STATE_LED_FLASH_1_T:  //闪烁一下
                    case STATE_LED_FLASH_2_T:  //闪烁两下
                    case STATE_LED_FLASH_3_T:  //闪烁三下
                        ledFlashFast[i] += 2 * (State - STATE_LED_FLASH_0_5HZ);
                        if (i % 2 == 0) {
                            ledState[i / 2].byte &= 0xf0;
                        } else {
                            ledState[i / 2].byte &= 0x0f;
                        }
                        break;
                }
            }
            if (value) {
                ledValueTemp |= 1 << i;
            } else {
                ledValueTemp &= ~(1 << i);
            }
            if (ledValueTemp ^ ledValue) {
                if (ledValueTemp & (1 << i)) {
                    ledSetState(i, GPIO_PIN_SET);
                } else {
                    ledSetState(i, GPIO_PIN_RESET);
                }
            }
            ledValue = ledValueTemp;
        }
    }
}
/**
 * @brief
 * @param  num   My Param doc
 * @param  state My Param doc
 */
static void ledSetState(unsigned char num, GPIO_PinState state)
{
    switch (num) {
        case LEDNUM00:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, state);
            break;
        case LEDNUM01:
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0, state);
            break;
        case LEDNUM02:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, state);
            break;
        case LEDNUM03:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, state);
            break;
        case LEDNUM04:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, state);
            break;
        case LEDNUM05:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, state);
            break;
        case LEDNUM06:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, state);
            break;
        case LEDNUM07:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, state);
            break;
        case LEDNUM08:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, state);
            break;
        case LEDNUM09:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, state);
            break;
        case LEDNUM10:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, state);
            break;
        case LEDNUM11:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, state);
            break;
        case LEDNUM12:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, state);
            break;
        case LEDNUM13:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, state);
            break;
        case LEDNUM14:
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, state);
            break;
        case LEDNUM15:
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, state);
            break;
        case LEDNUM16:
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, state);
            break;
        case LEDNUM17:
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, state);
            break;
        case LEDNUM18:
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, state);
            break;
        case LEDNUM19:
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, state);
            break;
        default:
            break;
    }
}
